package cn.dzhang.spring.boot.service.impl;

import javax.annotation.PostConstruct;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import cn.dzhang.spring.boot.model.Gender;
import cn.dzhang.spring.boot.model.User;
import cn.dzhang.spring.boot.repository.UserRepository;
import cn.dzhang.spring.boot.service.JpaAdapter;
import cn.dzhang.spring.boot.service.UserService;

@Service
public class UserServiceImpl extends JpaAdapter<User, Integer> implements
		UserService {
	private static final Log log = LogFactory.getLog(UserServiceImpl.class);
	@Autowired
	private UserRepository userRepository;

	@PostConstruct
	public void setup() {
		User user = new User();
		user.setName("章多亮");
		user.setUsername("dzhang");
		user.setGender(Gender.MALE);
		user.setPassword("kingdom");

		user = saveOrUpdate(user);
		log.info(user.getId());
	}

	@Override
	public User findByUsername(String username) {
		return userRepository.findByUsername(username);
	}

	@Override
	public User findByName(String name) {
		return userRepository.findByName(name);
	}

	@Override
	protected JpaRepository<User, Integer> getRepository() {
		return userRepository;
	}

}
